﻿namespace MapleRock.P2PEmulator.RGR
{
  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Diagnostics;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using System.Windows.Forms.DataVisualization.Charting;

  internal sealed partial class AvgTrafficAmountGraphPage : GraphPage
  {
    private bool isChartInitialized = false;

    public AvgTrafficAmountGraphPage()
      : base()
    {
      this.InitializeComponent();
    }

    public override void OnPageEnter()
    {
      base.OnPageEnter();

      if (!this.isChartInitialized)
      {
        this.FillChartSeriesData();

        this.isChartInitialized = true;
      }
    }

    protected override string YAxisName
    {
      get { return "Интенсивность потока траффика  через узел"; }
    }

    protected override string YAxisAbbr
    {
      get { return "Ravg"; }
    }

    private void FillChartSeriesData()
    {
      Debug.Assert(!this.isChartInitialized, "!this.isChartInitialized");

      var experiment = RgrData.Instance.Experiment;

      var min = new List<DataPoint>();
      var avg = new List<DataPoint>();
      var max = new List<DataPoint>();

      for (var sampleIndex = 0; sampleIndex < experiment.Samples.Length; ++sampleIndex)
      {
        var sample = experiment.Samples[sampleIndex];
        for (var runIndex = 0; runIndex < RgrExperiment.RunsPerSample; ++runIndex)
        {
          var run = sample.Runs[runIndex];

          Debug.Assert(run.MinCount.Parameter != 0, "run.MinCount.Parameter != 0");
          Debug.Assert(run.AvgCount.Parameter != 0, "run.AvgCount.Parameter != 0");
          Debug.Assert(run.MaxCount.Parameter != 0, "run.MaxCount.Parameter != 0");

          var minCount = new DataPoint(run.MinCount.Parameter, run.MinCount.PeerTrafficStreamIntencity);
          var avgCount = new DataPoint(run.AvgCount.Parameter, run.AvgCount.PeerTrafficStreamIntencity);
          var maxCount = new DataPoint(run.MaxCount.Parameter, run.MaxCount.PeerTrafficStreamIntencity);
          
          min.Add(minCount);
          avg.Add(avgCount);
          max.Add(maxCount);
        }
      }

      base.FillChartSeriesData(min, avg, max);
    }
  }
}
